from blinker import Namespace
from flask import current_app

from app.models.ctfModel import CtfRedBlueFlag
from tools import db

name_space = Namespace()
delete_flag_signal = name_space.signal("delete_red_blue_flag")


def delete_flag(sender, **kwargs):
    """删除场景库时删除导入的flag"""
    ctf_id = kwargs["ctf_id"]

    try:
        CtfRedBlueFlag.query.filter_by(ctf_red_blue_id=ctf_id).delete()
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(f"红蓝比赛: {ctf_id}，flag删除失败")
        current_app.logger.exception(e)


delete_flag_signal.connect(delete_flag)
